<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.su.middle.Utils"%>
<%@ page import="com.su.middle.services.ProposalsImpl"%>
<%@ page import="com.su.middle.services.Proposals"%>
<%@ page import="com.su.middle.bp.ProcessHelper"%>
<%@ page import="java.util.Map" %>
<jsp:useBean id="user" class="com.su.middle.User" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<%
if (!Utils.isLogged(user)) {
	response.sendRedirect("index.jsp");
}

if (request.getMethod().equals("POST")) {
	
	if (user.getType() == 0) { // students
		String supervisor = request.getParameter("supervisor");
		String title = request.getParameter("title");
		
		if (!supervisor.equals("") && !title.equals("")) {
			Proposals p = new Proposals(-1, "P", user.getName(), supervisor, title);
			ProcessHelper.createProposal(p);
		}
	} else { // teachers
		String action = request.getParameter("action");
		String taskId = request.getParameter("task_id");
		
		if (!taskId.equals("") && !action.equals("")) {
			if (action.equals("accept")) {
				ProcessHelper.acceptTask(taskId);
			} else {
				ProcessHelper.rejectTask(taskId);
			}
		}
	}
	
	response.sendRedirect("proposals.jsp");
}
%>

<jsp:include page="header.jsp" />

<% if (user.getType() == 0) { /* student's part */ %>
	
	<h2>Изпратени предложения</h2>
	<% Proposals[] data = ProposalsImpl.getStudentProposals(user.getName()); %>
	<% if (data != null && data.length != 0) { %>
		<table class="basic-table">
			<tr>
				<th>Заглавие</th>
				<th>Преподавател</th>
				<th>Статус</th>
			</tr>
		<% for (Proposals p : data) { %>	 
			<tr>
				<td><%= p.getThesisTitle() %></td>
				<td><%= p.getSupervisorName() %></td>
				<td><%= (p.getStatus().equals("P") ? "В очакване" : (p.getStatus().equals("R") ? "Отхвърлен" : "Приет")) %></td>
			</tr>
		<% } %>	
		</table>
	<% } else { %>
		<p>Няма изпратени предложения</p>
	<% } %>
	
	<br style="height: 80px;" />
	
	<h2>Ново предложение</h2>
	<form action="proposals.jsp" method="post">
	<div class="basic-form">
		<label for="supervisor">Преподавател</label>
		<input type="text" id="supervisor" name="supervisor" value="" />
		<br />
		
		<label for="title">Заглавие</label>
		<input type="text" id="title" name="title" value="" />
		<br />
		
		<input type="submit" value="Запис" />
	</div>
	</form>
	
<% } else { /* end if student' part, start teacher's part */ %>
	
	<h2>Предложения за дипломни работи</h2>
	<% Map<String, Proposals> data = ProcessHelper.getProposalsForSupervisor(user.getName()); %>
	<% if (data != null && data.size() != 0) { %>
		<table class="basic-table">
			<tr>
				<th>Заглавие</th>
				<th>Студент</th>
				<th>Статус</th>
			</tr>
		<% for (Map.Entry<String, Proposals> entry : data.entrySet()) { %>
			<tr>
				<td><%= entry.getValue().getThesisTitle() %></td>
				<td><%= entry.getValue().getStudentName() %></td>
				<td>
					<form action="proposals.jsp" method="post" style="float: left;">
					<div>
						<input type="hidden" name="task_id" value="<%= entry.getKey() %>" />
						<input type="hidden" name="action" value="accept" />
						<input type="submit" name="btn" value="Приемане" />
					</div>
					</form>
					<form action="proposals.jsp" method="post" style="float: left; margin-left: 20px;">
					<div>
						<input type="hidden" name="task_id" value="<%= entry.getKey() %>" />
						<input type="hidden" name="action" value="reject" />
						<input type="submit" name="btn" value="Отказване" />
					</div>
					</form>
				</td>
			</tr>
		<% } %>
		</table>
	<% } else { %>
		<p>Няма изпратени предложения</p>
	<% } %>

<% } /* end if teacher's part*/ %>

<br style="height: 80px;" />

<jsp:include page="footer.jsp" />